local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local PlayingGui = _G.ClientOHOH.load.UIManager():CreateUI("PlayingGui",require(script.PlayingGui))
local game_cfg=_G.CommonOHOH.config.Game
local wait2endGame=game_cfg.wait2endGame
local Players = game:GetService("Players")
local FunctionList = ClientOHOH.load.FunctionList();
local CallBackManager = _G.CommonOHOH.load.CallBackManager();
local playingPlayerList = {}
local connections = {}
local CountDonwConnect = nil
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
local fireworkEffect=ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.Effect.firework")
local reachedPlayers = {}
local Client_AudioManager = ClientOHOH.load.Client_AudioManager();
local soundId = "firework"
local LanguageHelper=_G.CommonOHOH.LanguageHelper 
function M.new(self,...)
	return M(self,...);
end;


function M.ctor(self, parent)
	M.super.ctor(self, parent)
    self.player=self.parent
    self.player.isWin = false
    self:AddConnect()
end

function M.AddConnect(self)
    self.player.main_inst.CharacterAdded:Connect(function(character)
        if  workspace:GetAttribute("GameState")==1 then
            self:State1Show()
        end
    end)

    _G.client.GameStateChangeEvent:Connect(function(state)
        if state==1 then
            self:State1Show()
        end
        if state==2 then
            PlayingGui:Hide()
            PlayingGui:ClearHeadshots()
            self:Clear()
        end
    end)

    self.player.DieEvent:Connect(function()
        PlayingGui:ClearHeadshots()
    end)

    _G.client.ReachTheEndEvent:Connect(function(newReachPlayers)
        print("到达终点")
        print(newReachPlayers) 
        local startIndex = #reachedPlayers + 1
        for index, value in pairs(newReachPlayers) do
            if value == self.player.user_id and self.player.isWin == false then
                self.player.isWin = true
                self:PlayFireWorkEffect()
            end
            if index >= startIndex then
                -- local name = Players:GetNameFromUserIdAsync(value)
                local name = index
              
                GlobalTipGui:Show(LanguageHelper:GetText(7,{"%%ARG1"},{name}))
            end
        end
        reachedPlayers = newReachPlayers
    end)
   
end

--游戏倒计时
function M.Countdown(self)
    local waitTime = wait2endGame- (os.time() - workspace:GetAttribute("StateTime"))
    CountDonwConnect = self:AddProcess(function()
            for time = waitTime, 1,-1 do
                PlayingGui:SetRemainTimeText(time)
                self:Wait(1)
            end
    end)
end

function M.InitPlayingPlayers(self)
    playingPlayerList = self.parent:Invoke(FunctionList.get_playing_players)
end
--获取正在游戏的玩家列表
function M.SetConnections(self)
    local index =1
    for _, player in pairs(playingPlayerList) do
        connections[index]=player:GetAttributeChangedSignal("ActivatedRoadSum"):Connect(function()
            PlayingGui:UpdateHeadshotPosition({[player.UserId] = player})
        end)
        index = index +1
    end
end

function M.State1Show(self)
    PlayingGui:Show()
    self:Countdown()
    self:InitPlayingPlayers()
    self:SetConnections()
    PlayingGui:InitHeadshots(playingPlayerList)
    PlayingGui:UpdateHeadshotPosition(playingPlayerList)
end

function M.Clear(self)
    self:ClearConnection()
    self:ClearList()
    self.player.isWin = false
    CountDonwConnect:Disconnect()
end

function M.ClearConnection(self)
    for _, value in pairs(connections) do
        value:Disconnect()
    end
end

--清除ActivatedRoadSum
function M.ClearAttribute(self)
    for _, player in pairs(playingPlayerList) do
        player:SetAttribute("ActivatedRoadSum",0)
    end
end

function M.ClearList(self)
    playingPlayerList = {}
    reachedPlayers = {}
end



function M.PlayFireWorkEffect(self)
    print("放烟花")
    local e=fireworkEffect:Clone()
    e.Parent = workspace
    assert(self.player.character,"self.character==null")
    local pos = CFrame.new(self.player.character:GetPosition()+Vector3.new(0,5,0))
    e.CFrame = pos
    e.Transparency = 1
    local animation = require(e.Animation)
    self:AddProcess(function()
        animation.play()
        Client_AudioManager:PlayToInst(soundId,e)
        self:Wait(1.5)
        animation.play()
        Client_AudioManager:PlayToInst(soundId,e)
        self:Wait(2)
        e:Destroy()
    end)
end

function M.dtor(self)
	M.super.dtor(self);
end;


return M